package com.sxzbxc.weixin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sxzbxc.weixin.entity.DicCompanies;
import com.sxzbxc.weixin.entity.DicLineinfo;
import com.sxzbxc.weixin.entity.DicSubstation;
import com.sxzbxc.weixin.entity.SysRole;
import com.sxzbxc.weixin.entity.SysUser;
import com.sxzbxc.weixin.pojo.AjaxJson;
import com.sxzbxc.weixin.service.DicCompaniseServiceI;
import com.sxzbxc.weixin.service.DicLineinfoServiceI;
import com.sxzbxc.weixin.service.DicSubstationServiceI;
import com.sxzbxc.weixin.service.SysRoleServiceI;
import com.sxzbxc.weixin.service.SysUserServiceI;

@Controller
@RequestMapping("volidformController")
public class VolidformController {

	
	@Autowired
	private DicCompaniseServiceI dicCompaniseService;
	
	@Autowired
	private DicSubstationServiceI dicSubstationService;
	
	@Autowired
	private DicLineinfoServiceI dicLineinfoService;
	
	@Autowired
	private SysUserServiceI sysUserService;
	
	@Autowired
	private SysRoleServiceI sysRoleService;
	
	/**
	 * 故障回馈--》单位编码校验
	 * @param req
	 * @param string
	 * @return
	 */
	@RequestMapping(params = "infoCompanies")
	@ResponseBody
	public AjaxJson infoCompanies(HttpServletRequest req , String string){
		AjaxJson j = new AjaxJson();
		List<DicCompanies> loadAll = dicCompaniseService.loadAll();
		List<String> list = new ArrayList<String>();
		for (DicCompanies dicCompanies : loadAll) {
			list.add(dicCompanies.getComCode());
		}
		if(list.contains(string)){
			j.setSuccess(true);
		}else{
			j.setSuccess(false);
		}
		return j;
	}
	
	
	/**
	 * 故障回馈--》变电站校验
	 * @param req
	 * @param string
	 * @return
	 */
	@RequestMapping(params = "infoSubstation")
	@ResponseBody
	public AjaxJson infoSubstation(HttpServletRequest req , String string){
		AjaxJson j = new AjaxJson();
		List<DicSubstation> loadAll = dicSubstationService.loadAll();
		List<String> list = new ArrayList<String>();
		for (DicSubstation dicSubstation : loadAll) {
			list.add(dicSubstation.getSubName());
		}
		if(list.contains(string)){
			j.setSuccess(true);
		}else{
			j.setSuccess(false);
		}
		return j;
	}
	
	/**.
	 * 故障回馈--》线路校验
	 * @param req
	 * @param string
	 * @return
	 */
	@RequestMapping(params = "infoLinecode")
	@ResponseBody
	public AjaxJson infoLinecode(HttpServletRequest req , String string){
		AjaxJson j = new AjaxJson();
		List<String> list = new ArrayList<String>();
		List<DicLineinfo> loadAll = dicLineinfoService.loadAll();
		for (DicLineinfo dicLineinfo : loadAll) {
			list.add(dicLineinfo.getLineName());
		}
		if(list.contains(string)){
			j.setSuccess(true);
		}else{
			j.setSuccess(false);
		}
		return j;
	}
	
	
	/**
	 * 人员信息--》手机验证
	 * @param request
	 * @param string
	 * @return
	 */
	@RequestMapping(params = "phonenum")
	@ResponseBody
	public AjaxJson phonenum(HttpServletRequest request , String string){
		AjaxJson j = new AjaxJson();
		List<String> list = new ArrayList<String>();
		List<SysUser> loadAll = sysUserService.loadAll();
		for (SysUser sysUser : loadAll) {
			list.add(sysUser.getPhonenum());
		}
		if(list.contains(string)){
			j.setSuccess(false);
		}else{
			j.setSuccess(true);
		}
		return j;
	}
	
	/**
	 * 验证权限
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "volidRole")
	@ResponseBody
	public AjaxJson volidRole(HttpServletRequest request ,String s_role){
		AjaxJson j = new AjaxJson();
		SysUser user = (SysUser)request.getSession().getAttribute("user");
		String userRole = user.getUserRole();
		if(userRole==""||userRole==null){
			j.setSuccess(false);
			return j;
		}
		SysRole role = sysRoleService.selectByPrimaryKey(userRole.trim());
		if(role.getRoleName().equals(s_role)){
			j.setSuccess(true);
			return j;
		}
		j.setSuccess(false);
		return j;
	}
	
	@RequestMapping(params = "departDataGrid")
	@ResponseBody
	public AjaxJson departDataGrid(HttpServletRequest request , String id){
		AjaxJson j = new AjaxJson();
		DicCompanies depart = dicCompaniseService.selectByPrimaryKey(id);
		j.setSuccess(true);
		j.setObj(depart);
		return j;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(params = "cityDatagrid")
	@ResponseBody
	public AjaxJson cityDatagrid(){
		AjaxJson j = new AjaxJson();
		List<DicCompanies> loadAll = dicCompaniseService.loadAll();
		Map<String , Object> map = new HashMap<String , Object>();
		for (DicCompanies dicCompanies : loadAll) {
			List<String> list = new ArrayList<String>();
			String comRegionName = dicCompanies.getComRegionName();//所有的县区
			if(map.containsKey(comRegionName)){//如果map的key值已存在该区域
				String comPlaceName = dicCompanies.getComPlaceName();
				list=(List<String>) map.get(comRegionName);
				list.add(comPlaceName);
				map.put(comRegionName, list);
				//map.put(comRegionName, null);
			}else{
				String comPlaceName = dicCompanies.getComPlaceName();
				list.add(comPlaceName);
				map.put(comRegionName, list);
			}
		}
		j.setSuccess(true);
		j.setObj(map);
		return j;
	}
}
